package com.study.fetch;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.alibaba.fastjson2.annotation.JSONField;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/11/26 21:25
 */
public class ClsDemo {

    Predicate<SubJect> predicate = subJect -> {
        //发布人是财联社
        boolean cls = StrUtil.isBlank(subJect.getArticleAuthor());
        //"震荡走高", "持续走高"
        boolean containAny = StringUtils.containsAny(subJect.getArticleTitle(), "投资舆情热点", "涨停分析");
        return cls && containAny;
    };

    public String subJectApp(String id, Predicate<SubJect> predicate) {
        Long articleTime = LocalDateTimeUtil.toEpochMilli(LocalDateTime.now()) / 1000;
        String url = "https://api3.cls.cn/api/subject/{}/article?last_article_time={}&rn={}&subject_id={}";

        String body = HttpRequest.get(StrUtil.format(url, id, articleTime, 130, id)).execute().body();
        String data = JSON.parseObject(body).getString("data");
        System.out.println(data);
        List<SubJect> subJects = JSON.parseArray(data, SubJect.class, Feature.SupportSmartMatch);
        //Predicate<SubJect> predicate = a -> StrUtil.isBlank(a.getArticleAuthor());
        List<SubJect> collect = subJects.stream().filter(predicate).filter(a -> a.getCTime().toLocalDate().isEqual(LocalDate.now())).collect(Collectors.toList());

        return collect.stream()//
                .map(s -> s.dingImg()).collect(Collectors.joining("\n\n"));
    }

    @Test
    public void ztfx() {
        subJectApp("1103", predicate);
    }

    @Data
    @Accessors(chain = true)
    class SubJect {
        @JSONField(name = "article_id")
        private String id;
        private Long articleTime;
        private String articleTitle;
        private String articleBrief;
        /**
         * 这个为空表示是财联社自己
         */
        private String articleAuthor;
        private String content;
        private LocalDateTime cTime;

        /**
         * app接口这个就是 投资舆情热点
         */
        private String articleImg;

        public SubJect setArticleTime(Long articleTime) {
            this.articleTime = articleTime;
            this.cTime = LocalDateTimeUtil.of(articleTime * 1000);
            return this;
        }

        public String ding() {

            return StrUtil.format("{} {}\n" + "{} {}\n" + "{} {}\n" + "{} {}\n" + "{}  \n" + "\n", Emo.hd_caidai, "财联社", Emo.jb, articleTitle, Emo.d, "https://www.cls.cn/detail/" + id, Emo.ygsz_y, content, LocalDateTimeUtil.formatNormal(cTime));
        }

        public String dingImg() {

            List<String> split = StrUtil.split(articleImg, ",");
            if (CollectionUtils.isEmpty(split)) {
                split = Lists.newArrayList();
            }

            return StrUtil.format("{} {}\n" + "{} {}\n" + "{} {}\n" + "{} {}\n" + "{}  \n" + "\n", Emo.hd_caidai, "财联社", Emo.jb, articleTitle, Emo.d, "https://www.cls.cn/detail/" + id, Emo.ygsz_y, String.join("\n", split), LocalDateTimeUtil.formatNormal(cTime));
        }
    }

    interface Emo {
        /**
         * 活动-彩带
         */
        String hd_caidai = "🎉";
        String ygsz_y = "👉";

        String jb = "🏆";

        String d = "📌";
    }
}
